import getProp from './prop'
import { formatAttribute } from './utils'
import { validateRequired } from '../rules/utils'

const TIPS_ADDRESS = '选择'

/**
 * 属性转换 format_type
 * @param {*} { format_type } 需要转换的type
 * @return {*} type=1-> 省
 */
function _formatAddress ({ format_type }) {
  // (("1", "省"), ("1-2", "省-市"), ("1-2-3", "省-市-区(县)"))
  const formatDate = [
    { type: '1', format: '1' },
    { type: '1-2', format: '2' },
    { type: '1-2-3', format: '3' },
  ]

  return formatAttribute(formatDate, format_type) || formatDate[0]
}

/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*}
 */
function _getRules ({ fields }) {
  const { label, required } = getProp({ fields })
  const { setting, verification } = fields
  const { format_type, has_fill } = setting
  const { type } = verification

  return {
    rules: [
      validateRequired(type, required, `请${TIPS_ADDRESS}${label}！`),
    ],
    columnsNum: _formatAddress({ format_type }),
    hasFill: !!has_fill,
  }
}

/**
 * 获取地址类型属性
 * @export
 * @param {*} { fields }
 * @return {*}
 */
export default function getAddressProp ({ fields }) {
  const prop = getProp({ fields })

  const rules = _getRules({ fields })

  return {
    ...prop,
    ...rules,
  }
}
